package com.qf.controller;

import com.qf.pojo.Emp;
import com.qf.pojo.PageHelper;
import com.qf.service.EmpService;
import com.qf.service.EmpServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 查询员工列表
 * @author 千锋健哥
 */
@WebServlet("/empList")
public class EmpListController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收分页查询条件
        String tempPage = req.getParameter("currentPage");
        Integer currentPage = 1;
        //判断如果当前页为空, 那么默认查询第一页数据
        if (tempPage == null || "".equals(tempPage)) {
            currentPage = 1;
        } else {
            //将接收到的参数当前页, 转成int类型
            currentPage = Integer.parseInt(tempPage);
        }
        //判断如果当前页小于等于0, 那么默认查询第一页数据
        if (currentPage <= 0) {
            currentPage = 1;
        }

        //创建分页对象实体
        PageHelper<Emp> pageHelper = new PageHelper<Emp>(currentPage);

        //调用service进行分页查询
        EmpService empService = new EmpServiceImpl();
        pageHelper = empService.findEmpPage(pageHelper);

        //2. 将数据放入请求域中, 带到页面展示
        List<Emp> list = pageHelper.getList();
        req.setAttribute("list", list);
        //将当前页数据, 回显到页面
        req.setAttribute("currentPage", currentPage);
        //封装的分页实体类
        req.setAttribute("pageHelper", pageHelper);
        //3. 跳转到列表页面
        req.getRequestDispatcher("/list.jsp").forward(req, resp);


    }

}
